package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.EivStatus;
import com.csjy.migration.hnnd.dict.RetreatType;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 电子发票
 */
@BusinessDataConverter(
        insertSql = "insert into apply_eticket_result (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, CHARGE_ID, C_INVOICEID, C_ORDERNO, C_JPG_URL, C_HJSE, C_BUYERNAME, C_FPHM, C_TAXNUM, C_URL, C_FPQQLSH, C_INVOICE_LINE, C_FPDM, C_RESULTMSG, C_BHSJE, C_MSG, C_STATUS, C_KPRQ, C_JYM, RETREAT_TYPE, IS_CANCEL, INVOICE_TAX_RATE, COMMODITY_ENCODING, AVAILABLE_AMOUNT)" +
                                        "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE apply_eticket_result SET CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,CHARGE_ID = ?,C_INVOICEID = ?,C_ORDERNO = ?,C_JPG_URL = ?,C_HJSE = ?,C_BUYERNAME = ?,C_FPHM = ?,C_TAXNUM = ?,C_URL = ?,C_FPQQLSH = ?,C_INVOICE_LINE = ?,C_FPDM = ?,C_RESULTMSG = ?,C_BHSJE = ?,C_MSG = ?,C_STATUS = ?,C_KPRQ = ?,C_JYM = ?,RETREAT_TYPE = ?,IS_CANCEL = ?,INVOICE_TAX_RATE = ?,COMMODITY_ENCODING = ?,AVAILABLE_AMOUNT = ? WHERE ID = ?",
        findMigratedIdListSql = "\n" +
            "SELECT ID FROM APPLY_ETICKET_RESULT aer \n" +
            "WHERE aer.CHARGE_ID IN\n" +
            "(\n" +
            "\tSELECT \n" +
            "\t\tID \n" +
            "\tFROM ND_FEE_COLLECTION_RECORD_TABLE nfcrt \n" +
            "\tWHERE NFCRT.PRO_ID = ?\n" +
            ")"
)
public class ApplyEticketResult  extends AbstractBusiness{

    public ApplyEticketResult(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }
    private static List<String> runningWorkflowCodeList = Arrays.asList(new String[]{
    });
    @Override
    public void migrate() throws ScriptException, SQLException, Exception {
        List<Map> btItemInVoiceList = sourceData.getBtItemInvoice();
        Map btItemBasic = sourceData.getBtItemBasic().get(0);
        List<Map> btItemFareDetailList = sourceData.getBtItemFareDetail();
        Map btItemFareDetail = btItemFareDetailList.isEmpty() ? null : sourceData.getBtItemFareDetail().get(0);

        for(Map btItemInVoice:btItemInVoiceList) {
            try {

                int needMigratedResult = this.needMigrated(btItemInVoice.get("EIVWKNO").toString());
                if (needMigratedResult == 0) {
                    continue;
                }
                // 单条插入或更新
                if (needMigratedResult == 1) {
                    this.targetDbUtil.updateByParams(updateSql, this.buildValues(btItemInVoice, btItemBasic,btItemFareDetail), true);
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchUpdateValues.add(this.buildValues(row));
                } else {
                    this.targetDbUtil.updateByParams(insertSql, this.buildValues(btItemInVoice, btItemBasic,btItemFareDetail));
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchInsertValues.add(this.buildValues(row));
                }
            } catch (Exception e) {
                throw new Exception(String.format("电子发票[%s]迁移失败:%s\n", btItemInVoice.get("EIVWKNO"), e.getMessage()));
            }


        }


}
    public List buildValues(Map btItemInVoice,Map btItemBasic,Map btItemFareDetail) throws SQLException, ScriptException {
        List values = new ArrayList();
        //ID
        values.add(btItemInVoice.get("EIVWKNO"));
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btItemInVoice.get("EIVCREATEDATE"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btItemInVoice.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(1);
        //CHARGE_ID	费用ID
        values.add(btItemFareDetail == null ? null : btItemFareDetail.get("CHARGENO"));
        //C_INVOICEID	发票主键
        values.add(btItemInVoice.get("EIVSTRWKNO"));
        //C_ORDERNO	订单编号
        values.add(btItemBasic.get("ITEMCODE"));
        //C_JPG_URL	发票图片地址
        values.add(btItemInVoice.get("EIVFILESTORENAME") == null || btItemInVoice.get("EIVFILESTORENAME").equals("") ? null
               : btItemInVoice.get("EIVFILESTORENAME").toString().replace("-",""));
        //C_HJSE	 合计税额
        values.add(0);
        //C_BUYERNAME	购方名称
        values.add(btItemInVoice.get("EIVPRONAME"));
        //C_FPHM	发票号码
        values.add(btItemInVoice.get("EIVNO"));
        //C_TAXNUM	购方税号
        values.add(btItemInVoice.get("EIVTAXREGISTCODE"));
        //C_URL	发票 pdf 地址
        values.add(btItemInVoice.get("EIVFILEURL"));
        //C_FPQQLSH	发票请求流水号
        values.add(null);
        //C_INVOICE_LINE	发票种类  1 法人发票 2个人发票
        values.add(btItemInVoice.get("EIVPROTYPE").equals("1") ? 0 : 1 );
        //C_FPDM	发票代码
        values.add(btItemInVoice.get("EIVCODE"));
        //C_RESULTMSG	结果信息
        values.add(Tool.decodeUnicode(btItemInVoice.get("EIVMEMO"))+Tool.decodeUnicode(btItemInVoice.get("BUSIMEMO")));
        //C_BHSJE	不含税金额
        values.add(btItemInVoice.get("EIVSUM"));
        //C_MSG	开票信息，成功或者失败的信息
        values.add(null);
        // C_STATUS	开票状态  1=待生成,2=已生成,3=不生成,4=已冲红
        values.add(DictTool.getTargetDictCode(EivStatus.class,btItemInVoice.get("EIVSTATUS")));
        //C_KPRQ	开票日期
        values.add(btItemInVoice.get("EIVCREATEDATE"));
        //C_JYM	校验码
        values.add(null);
        // RETREAT_TYPE	费用类型
        values.add(DictTool.getTargetDictCode(RetreatType.class,btItemInVoice.get("EIVCHARGETYPE")));
        //IS_CANCEL	作废状态
        values.add(null);
        //TODO INVOICE_TAX_RATE	发票税率 程序查询的是收费里的字段 taxRate
        values.add(btItemInVoice.get("EIVTAXRATE"));
        //TODO COMMODITY_ENCODING	商品编码  (NdInvoicetaxRateEntity chargesType ChargeType )
        values.add(btItemInVoice.get("EIVGOODSCODE"));
        //AVAILABLE_AMOUNT	可开票额
        values.add(null);
        return values;
    }
}
